package com.xxx.time;

import javax.lang.model.SourceVersion;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Set;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/19 16:25
 * @Copyright 博为峰
 */
public class TimeDemo01 {
    public static void main(String[] args) {
//        now();
//        of();
//        plus();
//        milli();
//        zone();
//        format();
//        period();//年月日
//        duration();//时分秒纳秒
//        chronounit();
        //Date --> Instant --> LocalDateTime
        Date date = new Date();
//        System.out.println("date = " + date);
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        System.out.println("localDateTime = " + localDateTime);

        //LocalDateTime --> Instant --> Date
        Instant toInstant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        Date date1 = Date.from(toInstant);
    }

    public static void chronounit() {
        LocalDate endDate = LocalDate.now();
        LocalDate startDate = LocalDate.of(2008, Month.AUGUST, 8);
        long l = ChronoUnit.DAYS.between(startDate, endDate);
        System.out.println("l = " + l);
    }

    public static void duration() {
        Instant instant1 = Instant.now();
        Instant instant2 = instant1.plusSeconds(10);
        System.out.println(Duration.between(instant1, instant2).toNanos());
    }

    public static void period() {
        LocalDate endDate = LocalDate.now();
        LocalDate startDate = LocalDate.of(2008, Month.AUGUST, 8);
        Period period = Period.between(startDate, endDate);
        System.out.println(period.getYears() + "年" + period.getMonths() + "月" + period.getDays() + "天");
    }

    public static void format() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(LocalDateTime.now());
        System.out.println("format = " + format);
    }

    public static void zone() {
        Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
        for (String availableZoneId : availableZoneIds) {
//            System.out.println("availableZoneId = " + availableZoneId);
        }

        String s = ZoneId.systemDefault().toString();
        System.out.println("s = " + s);

//        ZonedDateTime zonedDateTime = ZonedDateTime.now();
//        System.out.println("zonedDateTime = " + zonedDateTime);

        LocalDateTime localDateTime = LocalDateTime.now();
//        System.out.println("localDateTime = " + localDateTime);
        localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
    }

    public static void milli() {
        Instant instant = Instant.now();//时间戳
        long milli = instant.toEpochMilli();
        System.out.println(instant.toString());
        System.out.println("milli = " + milli);
    }

    public static void plus() {
        LocalDate localDate = LocalDate.now();
        System.out.println("localDate = " + localDate);
        LocalDate localDate1 = localDate.plusYears(5).plusDays(10);
        System.out.println("localDate1 = " + localDate1);
        LocalDate localDate2 = localDate.minusDays(10);
        System.out.println("localDate2 = " + localDate2);


//        LocalTime localTime = LocalTime.now();
//        System.out.println("localTime = " + localTime);
//        LocalTime localTime1 = localTime.plusHours(2);
//        System.out.println("localTime1 = " + localTime1);
    }

    public static void of() {
        LocalDate localDate = LocalDate.of(2018, 8, 8);
        System.out.println("localDate = " + localDate);
        LocalTime localTime = LocalTime.of(12, 20, 36);
        System.out.println("localTime = " + localTime);
        LocalDateTime localDateTime = LocalDateTime.of(2021, 7, 20, 9, 29, 56);
        System.out.println("localDateTime = " + localDateTime);
    }

    public static void now() {
        Instant instant = Instant.now();//时间戳
        System.out.println("instant = " + instant);
        LocalDate localDate = LocalDate.now();//日期
        System.out.println("localDate = " + localDate);
        LocalTime localTime = LocalTime.now();//时间
        System.out.println("localTime = " + localTime);
        LocalDateTime localDateTime = LocalDateTime.now();//日期时间
        System.out.println("localDateTime = " + localDateTime);
        ZonedDateTime zonedDateTime = ZonedDateTime.now();//时区
        System.out.println("zonedDateTime = " + zonedDateTime);
    }
}
